﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using SprentCMS.Core.Models.ViewDatas;

namespace SprentCMS.Mvc.ViewEngines {
    public class ManagerSelectViewEngine : VirtualPathProviderViewEngine {
        public ManagerSelectViewEngine() {

        }
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) {
            return new WebFormView(partialPath, null);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) {
            return new WebFormView(viewPath, masterPath);
        }

        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) {
            string templatename = null;
            if (controllerContext.Controller.ViewData.Model is IBaseModuleViewData) {
                IBaseModuleViewData baseViewData = controllerContext.Controller.ViewData.Model as IBaseModuleViewData;
                templatename = baseViewData.TemplateName;
            }

            if (!string.IsNullOrEmpty(templatename)) {
                /* {0} = view name or master page name 
                 * {1} = controller name 
                 */

                // create our master page location

               // controllerContext.RouteData.Values["controller"] = "";

                MasterLocationFormats = new[] {
                "~/Templates/"+templatename+"/{0}Master.master",
                "~/Views/{1}/{0}.master",
                "~/Views/Shared/{0}.master"
                };

                ViewLocationFormats = new[] {
                "~/Templates/"+templatename+"/{0}.aspx",
                "~/Templates/"+templatename+"/{0}.ascx",
                "~/Views/Shared/{0}.aspx",
                "~/Views/Shared/{0}.ascx"
                 };

                PartialViewLocationFormats = ViewLocationFormats;
            }
            else {
                /* {0} = view name or master page name 
                 * {1} = controller name 
                 */

                // create our master page location  
                MasterLocationFormats = new[] {
                    "~/Views/{1}/{0}.master",
                    "~/Views/Shared/{0}.master"
                     };

                ViewLocationFormats = new[] {
                    "~/Views/{1}/{0}.aspx",
                    "~/Views/{1}/{0}.ascx",
                    "~/Views/{1}/{0}.aspx",
                    "~/Views/{1}/{0}.ascx",
                    "~/Views/Shared/{0}.aspx",
                    "~/Views/Shared/{0}.ascx"
                    };

                PartialViewLocationFormats = ViewLocationFormats;
            }
            return base.FindView(controllerContext, viewName, masterName, false/* 当相同Controller、Viewname等参数相同时，可能会返回不同页面时，不使用缓存 */);

        }

        //if (viewName.IndexOf("_") != -1)
        //{
        //    string[] viewNamePath = viewName.Split('_');
        //    string managerPath = viewNamePath[0];
        //    viewName = string.Join("_", viewNamePath, 1, viewNamePath.Length - 1);

        //    /* {0} = view name or master page name 
        //     * {1} = controller name 
        //     */

        //    // create our master page location  
        //    MasterLocationFormats = new[] {
        //    "~/Views/"+managerPath+"/{1}/{0}.master",
        //    "~/Views/Shared/{0}.master"
        //    };

        //    ViewLocationFormats = new[] {
        //    "~/Views/"+managerPath+"/{1}/{0}.aspx",
        //    "~/Views/"+managerPath+"/{1}/{0}.ascx",
        //    "~/Views/Shared/{0}.aspx",
        //    "~/Views/Shared/{0}.ascx"
        //     };

        //    PartialViewLocationFormats = ViewLocationFormats;
        //}
        //else
        //{
        //    /* {0} = view name or master page name 
        //     * {1} = controller name 
        //     */

        //    // create our master page location  
        //    MasterLocationFormats = new[] {
        //    "~/Views/{1}/{0}.master",
        //    "~/Views/Shared/{0}.master"
        //     };

        //    ViewLocationFormats = new[] {
        //    "~/Views/{1}/{0}.aspx",
        //    "~/Views/{1}/{0}.ascx",
        //    "~/Views/Shared/{0}.aspx",
        //    "~/Views/Shared/{0}.ascx"
        //    };

        //    PartialViewLocationFormats = ViewLocationFormats;
        //}
        //// System.Web.HttpContext.Current.Response.Write("<br / >" + viewName + "," + ViewLocationFormats[0]);
        //return base.FindView(controllerContext, viewName, masterName, false/* 当相同Controller、Viewname等参数相同时，可能会返回不同页面时，不使用缓存 */);
    }
}

